package com.thread.plbc;

/**
 * @author げっしょく
 * @date 2021/8/6
 */
public class ThreadFoodie7 extends Thread{
    private ThreadDest7 td;
    public ThreadFoodie7(ThreadDest7 td){
        this.td = td;
    }
    @Override
    public void run() {
        while (true) {
            if (td.getCount() == 0) {
                break;
            }else {
                synchronized (td.getObj()){
                    if(td.isFlag()){
                        System.out.println("消费者吃了一顿");
                        int count = td.getCount();
                        count--;
                        td.setCount( count );
                        td.setFlag(false);
                        td.getObj().notifyAll();
                    }else {
                        try {
                            td.getObj().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

class Coolde extends Thread{
    private ThreadDest7 td;
    public Coolde(ThreadDest7 td){
        this.td = td;
    }
    @Override
    public void run() {
        while (true) {
            if(td.getCount() == 0){
                break;
            }else {
                synchronized (td.getObj()){
                    if(!td.isFlag()){
                        System.out.println("生产者又做了一顿大餐");
                        td.setFlag(true);
                        td.getObj().notifyAll();
                    }else {
                        try {
                            td.getObj().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

    }
}